package cn.blankchn.www.util;

import cn.blankchn.www.entity.HandleResult;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * Created by apple on 2018/4/2.
 */
public class WriteCsvFactory {

    public static void write(HandleResult result, String strPath, int num) throws IOException {


        System.out.println(strPath);
        String content = handleStr(result, num);
//        Files.write(Paths.get("/Users/apple/Desktop/results_test.csv"), content.getBytes());

        FileWriter fw = null;
        try {
//如果文件存在，则追加内容；如果文件不存在，则创建文件
            System.out.println(strPath);
            File f=new File(strPath);
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(content);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String handleStr(HandleResult result, int num) {

        StringBuilder sb = new StringBuilder();
        sb.append(num + ",")
                .append(result.getAvgSpeed() + ",")
                .append(result.getAvgAcce() + ",")
                .append(result.getAvgDown() + ",")
                .append(result.getPersistentTime() + ",")
                .append(result.getMaxAcce() + ",")
                .append(result.getMaxDown() + ",")
                .append(result.getAcceTime() + ",")
                .append(result.getDownTime() + ",")
                .append(result.getZeroTime() + ",")
                .append(result.getMaxSpeed() + ",")
                .append(result.getVs() + ",")
                .append(result.getVa() + ",")
                .append(result.getUniformTime() + "\r");
        return sb.toString();

    }


}
